<?php

/**
 * 兑奖管理 controller
 * @author alang
 */

class Exchange extends CI_Controller
{
	
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('exchange_mdl','exchange');
	}
	
	/**
	 * 
	 * 默认方法，兑奖列表 ...
	 */
	public function index()
	{
		
		$exchange = array();
		$exchange = $this->exchange->get_exchange_list();

		$ptype = intval($this->input->get('ptype'));
		$exchangestate = intval($this->input->get('exchangestate'));
		$starttime = $this->input->get('starttime');
		$endtime = $this->input->get('endtime');
		
		
		foreach ($exchange as $key => $value) {
			
			if ($ptype && $value['ptype'] !=$ptype) {
				
				unset($exchange[$key]);
			}	

			if ($exchangestate && $value['exchangestate'] !=$exchangestate) {
				
				unset($exchange[$key]);
			}
			
			if ( !empty($starttime) && !empty($endtime)) {
				
				$start = strtotime($starttime);
				$end = strtotime($endtime);
				if($start > $value['createtime'] || $end < $value['createtime']){
					unset($exchange[$key]);
				}
			}
		}
		
		$data['exchangestate'] = intval($exchangestate);
		$data['ptype'] = intval($ptype);
		$data['list'] = $exchange;
		$this->load->view('adminroot/list_exchange',$data);	
	}
	
	public function sendorder()
	{

		$expressor = $this->input->post('expressor');
		$tracenumber = $this->input->post('tracenumber');
		$id = intval($this->input->post('id'));

		if(!empty($expressor) && !empty($tracenumber) && !empty($id)){
			$data['expressor'] = $expressor;
			$data['tracenumber'] = $tracenumber;
			$data['shipdate'] = time();	
			$data['exchangestate'] = 2;
			echo $this->exchange->exchange_expreesor($id,$data);
		}else{
			echo 'asdfsaf';
		}
	}
}